/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;

/**
 *
 * @author Muhammad Anis
 */
public class subcategory {
    //memiliki beberapa room
    public List<room> room = new ArrayList<room>();
    //memiliki nama
    public String nama;
    //memiliki moderator
    public String moderator;
    public String visibility="visible";

    //konstruktor
    public subcategory(String nama,String moderator){
        this.nama = nama;
        this.moderator = moderator;
    }

    //menambahkan room
    public void addRoom(String nama,String moderator){
        room.add(new room(nama, moderator));
    }

    public void addRoom(room r){
        room.add(r);
    }

    //memprint
    public void print(JspWriter out){
        try {
            //print namanya
            String visible,height,img;
                if(this.visibility.equals("visible")){
                    visible = "visible";
                    height="auto";
                    img="min";
                }
                else{
                    visible = "hidden";
                    height="0px";
                    img="plus";
                }
            out.println("<div class=\"subcategory\">"
                    + "<a href=\"javascript:void(0);\" onclick=\"showHideDiv('"+this.nama+"')\"><img id=\""+this.nama+"_images\""+" src=\"images/"+img+".png\"/></a>"
                        + "<span onclick=\"chooseCategory('"+this.nama+"');\">"+this.nama+"</span></div>");
            out.println("<div  id=\""+this.nama+"\" style=\"visibility:"+visible+";height:"+height+";\">");
            for (room r : room) {
                r.print(out);
            }
            out.println("</div>");
        }
        //update, setiap room akan mengupdate isinya
        catch (IOException ex) {
            Logger.getLogger(subcategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //update, setiap room akan mengupdate isinya
    public void setVisibility(String status){
        this.visibility = status;
    }
    
    public boolean addUserinRoom(String roomname,String username){
        for(room r :room){
            if(r.nama.equals(roomname)){
                r.addUser(username);
                return true;
            }
        }
        return false;
    }

    public boolean addChatinRoom(String username,String roomaName,String message){
        for(room r : room){
            if(r.nama.equals(roomaName)){
                r.addChat(username, message);
                return true;
            }
        }
        return false;
    }
    
    public room getRoom(String roomname){
        for(room r : room){
            if(r.nama.equals(roomname))
                return r;
        }
        return null;
    }
    
    public List<room> getAllRoom(){
        return room;
    }
    
    
}
